﻿using System;
using System.Data.Services;
using System.Globalization;
using System.Linq;
using System.ServiceModel.Web;
using System.Data.Objects.DataClasses;
using System.Collections.Generic;

namespace RentAWorker.RIA.Web.Data
{
	public class RentAWorkerDataService : DataService<BigDays2009Entities>
    {
        public static void InitializeService(IDataServiceConfiguration config)
        {
			// everyone is allowed to read
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
			config.SetServiceOperationAccessRule("GetRentals", ServiceOperationRights.All);
        }

		[WebGet]
		public IQueryable<RentalService> GetRentals(int machineTypeId, string date)
		{
			// ADO.NET Data Services only support primitive data types for parameters.
			// Therefore we have to convert date-string into date (we use ISO 8601 format
			// here).
			var fromDate = DateTime.ParseExact(date, "s", CultureInfo.InvariantCulture);
			var toDate = fromDate.AddDays(1);

			// Get results from database
			return
				from rs in this.CurrentDataSource.RentalService
				where rs.RentResource.MachineType.MachineTypeID == machineTypeId
					&& rs.StartDate <= toDate
					&& rs.EndDate >= fromDate
				select rs;
		}
    }
}
